package com.blue.controller.workflow;

import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.Relation;
import com.blue.common.utils.ThreadLocalUtils;
import com.blue.vo.workflow.TaskInstanceVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.workflow.TaskInstancePO;
import com.blue.dto.workflow.TaskInstanceDTO;
import com.blue.service.workflow.ITaskInstanceService;
import com.blue.common.entity.ResponseResult;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;


/**
 * 流程任务实例表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/workflow/taskInstance")
@Tag(name = "流程任务实例表")
public class TaskInstanceController {
    @Autowired
    private ITaskInstanceService taskInstanceService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(TaskInstanceDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        TaskInstancePO taskInstancePO = ConvertUtils.convert(TaskInstancePO.class, requestDTO);
        String userId = ThreadLocalUtils.getLoginUserInfo().getUserId();
        taskInstancePO.setAssigneeId(userId);
        Relation relation1 = new Relation("start_time", "workflow_instance t1 on t.instance_id = t1.id");
        Relation relation2 = new Relation("definition_nm", "workflow_definition t2 on t1.definition_id = t2.id");
        Relation relation3 = new Relation("username as initiator", "author_user t3 on t1.initiator_id = t3.user_id");
        MPJQueryWrapper<TaskInstancePO> queryWrapper = QueryWrapperUtils.initQueryWrapper(taskInstancePO, "t.id,t.instance_id,t.status,t.operation_time,t.create_time", "t.create_time", CommonConstant.DESC, relation1, relation2, relation3);
        queryWrapper.eq(StringUtils.isNotBlank(requestDTO.getWfNo()),"t2.wf_no", requestDTO.getWfNo());
        queryWrapper.eq(StringUtils.isNotBlank(requestDTO.getInitiatorId()),"t3.user_id", requestDTO.getInitiatorId());
        Page<TaskInstancePO> page = new Page<>(pageNum, pageSize);
        Page<TaskInstancePO> pageResult = taskInstanceService.page(page, queryWrapper);
        taskInstanceService.computeTime(requestDTO.getStatus(),pageResult.getRecords());
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/taskNodeList")
    @Operation(summary = "获取任务节点信息")
    public ResponseResult<?> taskNodeList(@RequestBody TaskInstanceDTO requestDTO) {
        List<TaskInstanceVO> taskInstanceVOS = taskInstanceService.taskNodeList(requestDTO.getInstanceId());
        return ResponseResult.okResult(taskInstanceVOS);
    }
}
